﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication2.Models;

namespace WebApplication2.Controllers
{
    public class StudentController : Controller
    {
        // GET: Student
        protected studentModel db = new studentModel();//实例化数据访问上下文
        public ActionResult Index()
        {
            
            var list = db.stuInfo.ToList();//拿到stuInfo表中的所有数据
            return View(list);
        }

        // GET: Student/Details/5
        public ActionResult Details(int id)
        {
            var useinfo = db.stuInfo.FirstOrDefault(e => e.ID == id);
            return View(useinfo);
        }

        // GET: Student/Create
        public ActionResult Create()
        {
                List<classInfo> classinfo = db.classInfo.ToList();
                SelectList selectlist = new SelectList(classinfo, "claaID", "className");
                ViewBag.selectlist = selectlist.AsEnumerable();
                return View();
            
            
        }

        // POST: Student/Create
        [HttpPost]
        public ActionResult Create(stuInfo info)
        {
            try
            {
                // TODO: Add insert logic here
              
                db.stuInfo.Add(info);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: Student/Edit/5
        public ActionResult Edit(int id)
        {
            List<classInfo> classinfo = db.classInfo.ToList();
            SelectList selectlist = new SelectList(classinfo, "claaID", "className");
            ViewBag.selectlist = selectlist.AsEnumerable();
            var model = db.stuInfo.Where(e => e.ID == id).FirstOrDefault();
            ViewData.Model = model;
            return View();
        }

        // POST: Student/Edit/5
        [HttpPost]
        public ActionResult Edit(stuInfo info)
        {
            try
            {
                // TODO: Add update logic here

                db.stuInfo.Attach(info);
                db.Entry(info).State = System.Data.Entity.EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: Student/Delete/5
        public ActionResult Delete(int id)
        {
            var model = db.stuInfo.Where(e => e.ID == id).FirstOrDefault();
            return View(model);
        }

        // POST: Student/Delete/5
        [HttpPost]
        public ActionResult Delete(int id, stuInfo info)
        {
            try
            {
                // TODO: Add delete logic here
                db.Entry(info).State = System.Data.Entity.EntityState.Deleted;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
